<template>
	<view v-if="info== ''">
		<xbd-zwnz marginTop='300'></xbd-zwnz>
	</view>
	<view v-else class="center">
		<xbd-page-card>
			<template>
				<view>
					<xbd-image-title :title="info.name" :image="require('@/static/image/supervision/jdh.png')">
					</xbd-image-title>
					<view class="tag">
						活动类型
					</view>
					<view class="text">
						{{info.type}}
					</view>
					<view class="tag">
						创建人
					</view>
					<view class="text">
						{{info.creator_id.admin_name}}
					</view>
					<view class="tag">
						创建时间
					</view>
					<view class="text">
						{{info.created_at | timeFormat('YYYY-MM-DD HH:mm')}}
					</view>
					<view class="tag">
						活动描述
					</view>
					<view class="text">
						{{info.desc||'-'}}
					</view>
					<view class="tag">
						任务时间
					</view>
					<view class="time">
						{{info.start_time | timeFormat('YYYY-MM-DD HH:mm')}}
						<view class="to">
							至
						</view>
						<text v-if=" ( timeFormat(info.end_time) < nowDate)" style="color: #FC4F4F;">
							{{info.end_time | timeFormat('YYYY-MM-DD HH:mm')}}
						</text>
						<text v-else>
							{{info.end_time | timeFormat('YYYY-MM-DD HH:mm')}}
						</text>
					</view>
					<view class="tag">
						上报单位
					</view>
					<view class="text">
						{{info.department}}
					</view>
					<!-- 重传 -->

					<view v-if="info.state==='break'">
						<view class="tag">
							审核人
						</view>
						<view class="text">
							{{info.last_audit}}
							<text class="back" v-if="info.state==='break'">
								已退回
							</text>
						</view>
						<view class="tag">
							审核意见
						</view>
						<view class="text">
							{{info.view || "-"}}
						</view>
						<view class="tag">
							审核时间
						</view>
						<view class="text">
							{{info.audit_time | timeFormat('YYYY-MM-DD HH:mm')}}
						</view>
					</view>
				</view>
			</template>
		</xbd-page-card>
		<!-- 未审批 未上报或重新上报 -->
		<xbd-page-card v-if="info.state ==='wait' || info.state ==='break'">
			<template>
				<view>
					<view class="tag">
						上报人
					</view>
					<view class="text">
						{{userInfo.name}}
					</view>
					<view class="tag">
						<text style="color:#FC4F4F">*</text>资料上报
					</view>
					<view class="image">
						<view class="box-image" v-for="(item,index) in upImg" :key="index">
							<image class="del" src="@/static/image/del.png" @click="del(item)"></image>
							<image class="images" :src="item" @click="image_click(item)"></image>
						</view>
						<view v-if="upImg.length !==6" class="upload" @click="choose_image">
							<image src="@/static/image/supervision/upload.png" mode=""></image>
							<view class="">
								上传图片
							</view>
						</view>
					</view>
					<textarea class="textarea" placeholder="请输入内容..." v-model="value" maxlength="-1"
						auto-height></textarea>
				</view>
			</template>
		</xbd-page-card>
		<!-- 查看 -->
		<xbd-page-card v-if="info.state !=='wait' &&  info.state!=='break'">
			<template>
				<view>
					<view class="tag">
						上报人
					</view>
					<view class="text">
						{{info.last_report}}
					</view>
					<view class="tag">
						资料上报
					</view>
					<!-- 富文本编译插件 -->
					<parser v-if="info.content" :html="info.content" :tagStyle='style' />
					<view v-else>
						<view class="image">
							<view class="images" v-for="(item,index) in info.app_img " :key="index">
								<image @click="image_click(item)" :src="item"></image>
							</view>
						</view>
						<text>{{info.app_string}}</text>
					</view>
					<view class="">
						<view class="tag">
							上报时间
						</view>
						<view class="text">
							{{info.report_time | timeFormat('YYYY-MM-DD HH:mm')}}
							<text class="text-color" v-if="(info.report_time > info.end_time)">超时</text>
						</view>
					</view>

				</view>
			</template>
		</xbd-page-card>
		<!-- 已审批 查看 -->
		<xbd-page-card v-if="info.state!=='pending' &&  info.state!=='wait' &&  info.state!=='break'">
			<template>
				<view class="center_box">
					<image v-if="info.state === 'refused'" class="img" src="@/static/image/supervision/refused.png">
					</image>
					<image v-if="info.state === 'successed'" class="img" src="@/static/image/supervision/pass.png">
					</image>
					<view class="tag">
						审核人
					</view>
					<view class="text">
						{{info.last_audit}}
					</view>
					<view class="tag">
						审核意见
					</view>
					<view class="text">
						{{info.view || "-"}}
					</view>
					<view class="tag">
						审核时间
					</view>
					<view class="text">
						{{info.audit_time | timeFormat('YYYY-MM-DD HH:mm')}}
					</view>
				</view>
			</template>
		</xbd-page-card>
		<xbd-popup :isShow="showDialog" @dialogConfirm="dialogConfirm" @dialogCancel="dialogCancel"
			content="将撤回所有已上报内容，确定取消上报？" cancelText="取消" confirmText="确定"></xbd-popup>
		<xbd-button v-if="info.state==='wait' || info.state==='break'" text="提交" @clickBtn="submit()"></xbd-button>
		<xbd-button v-if="info.is_show" text="取消上报" @clickBtn="clickCancel()"></xbd-button>
	</view>

</template>

<script>
	import parser from '@/components/jyf-parser/jyf-parser.vue';
	import {
		Mission,
	} from '@/api/index.js';
	import {
		mapState
	} from 'vuex';
	const dayjs = require("dayjs");
	export default {
		components: {
			parser
		},
		data() {
			const nowDate = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
			return {
				showDialog: false,
				nowDate: nowDate,
				value: "",
				id: '',
				info: '',
				flag: true,
				upImg: [],
				style: {
					'img': 'display:block;margin: 0 auto;max-width:100%;height:auto; transform: translateX(0);',
					// 'rich-text': 'white-space: pre-line !important;word-break: break-all !important;word-wrap: break-word !important;'
					'video': 'width:100%;height:auto;',
				},
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		methods: {
			timeFormat(time) {
				return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
			},
			del(val) {
				console.log(val)
				this.upImg = this.upImg.filter(item => {
					return item !== val
				})
			},
			dialogConfirm() {
				console.log('确认')
				if(this.flag){
					this.flag = false
					this.cancel()
				}
				
				this.showDialog = false
			},
			clickCancel(){
				this.showDialog = true
			},
			dialogCancel() {
				this.showDialog = false
			},
			async cancel() {
				const res = await Mission.getTaskCancel(this.id)
				if (!res.success) {
					this.flag = true

				} else {
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						});
					}, 1500)
				}
				this.$showToast(res.msg);
			},
			image_click(img) {
				// 点击预览图片
				dd.previewImage({
					current: 1,
					urls: [img],
				});
				console.log(img, 'img')
			},
			choose_image() {
				uni.chooseImage({
					count: 6 - this.upImg.length,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log(chooseImageRes, 'chooseImageRes.tempFilePaths;')
						for (let i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: 'http://113.135.192.25:18398/api/upload', //仅为示例，非真实的接口地址
								header: {
									'Content-Type': 'multipart/form-data',
									'Authorization': uni.getStorageSync("token")
								},
								filePath: tempFilePaths[i],
								fileType: 'image',
								name: 'file',
								success: (uploadFileRes) => {
									if (this.upImg.indexOf(JSON.parse(uploadFileRes.data).data
										.url) == -1 && (JSON.parse(uploadFileRes.data).data.url
											.indexOf('.jpg') !== -1 || JSON.parse(uploadFileRes
												.data).data.url.indexOf('.png') !== -1 || JSON
											.parse(uploadFileRes.data).data.url.indexOf(
											'.jpeg') !== -1)) {
										this.upImg.push(JSON.parse(uploadFileRes.data).data.url)
									}
								}
							});
						}

					}
				});
			},
			submit() {
				this.flag = false
				if (this.upImg.length === 0 && this.value.length === 0) {
					this.flag = true
					this.$showToast('操作失败，请填写所有必填内容！');
				} else {
					this.postCircular()
				}
			},
			async postCircular() {
				const o = {
					id: this.id,
					result: '',
					app_img: this.upImg,
					app_string: this.value
				}
				const res = await Mission.postActivityReport(o)
				if (!res.success) {
					this.flag = true

				} else {
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						});
					}, 1500)
				}
				this.$showToast(res.msg);
			},
			async init() {
				const res = await Mission.getReportDetail(this.id)
				if(res.success){
					this.info = res.data
				}else{
					this.$showToast(res.msg);
				}
				console.log(this.info, 'this.info')
			}
		},
		async onLoad(res) {
			this.id = res.id
			console.log(res)
			await this.init()
		}
	}
</script>

<style lang="scss" scoped>
	.center {

		padding: 32rpx;
		font-size: 30rpx;
		line-height: 52rpx;

		.center_box {
			position: relative;

			.img {
				position: absolute;
				right: 30rpx;
				top: 40rpx;
				width: 120rpx !important;
				height: 100rpx !important;

			}
		}

		.tag {
			color: #A0A7B8;
			font-size: 26rpx;
			// margin-bottom: 20rpx;

		}

		.text {
			margin-bottom: 20rpx;
			white-space: pre-line !important;
			word-break: break-all !important;
			word-wrap: break-word;

			.text-color {
				margin-left: 20rpx;
				padding: 3rpx 6rpx;
				background: rgba(252, 185, 79, 0.15);
				border-radius: 4rpx;
				color: #FCB94F;
			}

			.back {
				color: #FFFFFF;
				padding: 3rpx 6rpx;
				margin-left: 30rpx;
				background-color: #FC4F4F !important;
				font-size: 22rpx;
				border-radius: 6rpx;
			}
		}

		.image {
			display: flex;
			flex-wrap: wrap;
			margin-bottom: 20rpx;

			.box-image {
				position: relative;
			}

			.del {
				width: 38rpx;
				height: 38rpx;
				position: absolute;
				top: -16rpx;
				right: 4rpx;
			}

			.images {
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				width: 140rpx;
				height: 140rpx;

				image {
					width: 140rpx;
					height: 140rpx;
				}
			}

			.upload {

				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				font-size: 20rpx;
				color: #A0A7B8;
				width: 140rpx;
				height: 140rpx;
				border: 2rpx dashed #d0d6e5;
				border-radius: 8rpx;
				line-height: normal;

				image {
					width: 28rpx;
					height: 28rpx;
					margin-right: 0;
					margin-bottom: 10rpx;
				}
			}
		}

		.time {
			display: flex;

			.to {
				color: #A0A7B8;
				margin: 0 20rpx;
			}
		}

		.textarea {
			min-height: 160rpx;
			padding: 20rpx 30rpx;
			font-size: 30rpx;
			border: 2rpx solid #EBEEF5;
			margin-bottom: 20rpx;
			border-radius: 8rpx;
		}

		.floot {
			margin-top: 60rpx;
			display: flex;
			justify-content: space-between;

			.btn {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 200rpx;
				height: 90rpx;
				font-size: 32rpx;
				border-radius: 8rpx;
			}

			.refused {
				border: 2rpx solid #FC4F4F;
				color: #FC4F4F;
			}

			.back {
				border: 2rpx solid #FCB94F;
				color: #FCB94F;
			}

			.pass {
				background: linear-gradient(266deg, #2e73fc 3%, #1ea9fb 95%);
				// border: 2rpx solid #FC4F4F;
				color: #FFFFFF;
			}
		}
	}
</style>
